<template>
    <el-container class="teacher">
        <el-page-header @back="goBack" :content="`${title} - 讲师管理`">
        </el-page-header>
        <div class="teacher-header">
            <el-button size="small" type="primary" @click="openDialog('add')">添加讲师</el-button>
        </div>

        <el-main class="teacher__content">
            <div ref="container" class="table-container">
                <el-table ref="table" :data="datas" style="width: 100%" align="center" header-align="center">
                    <el-table-column label="讲师姓名" prop="name" width="100px" center />
                    <el-table-column label="讲师头衔" prop="title" width="160px" center />
                    <el-table-column label="讲师头像" width="160px" center>
                        <template slot-scope="scope">
                            <el-image :src="scope.row.avatar" style="width:100px;height:100px;border-radius:50%" />
                        </template>
                    </el-table-column>
                    <el-table-column label="讲师介绍" prop="desc" center />

                    <el-table-column label="操作" center>
                        <template slot-scope="scope">
                            <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
                            <el-button size="small" @click="openDialog('edit', scope.row)">更改</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </el-main>

        <el-dialog :title="dialogText" :visible.sync="dialogVisible" width="600px" center :lock-scroll="true" top="40px"
            :close-on-press-escape="false" :close-on-click-modal="false" v-if="dialogVisible">
            <el-form label-width="160px">
                <el-form-item label="讲师姓名" required>
                    <el-input v-model="formData.name" placeholder="请输入讲师标题" />
                </el-form-item>

                <el-form-item label="讲师头衔" required>
                    <el-input v-model="formData.title" placeholder="请输入讲师头衔" />
                </el-form-item>

                <el-form-item label="讲师头像" required>
                    <upload-picture :url.sync="formData.avatar" prefix="topic/teacher"></upload-picture>
                    <span> (建议头像宽高比为1:1) </span>
                </el-form-item>

                <el-form-item label="讲师介绍">
                    <el-input v-model="formData.desc" type="textarea" placeholder="请输入讲师简介" />
                </el-form-item>
            </el-form>
            <span slot="footer">
                <el-button @click="cancel">取消</el-button>
                <el-button type="primary" @click="handleUpdate">保存</el-button>
            </span>
        </el-dialog>
    </el-container>
</template>

<script>
import uploadPicture from "@/components/uploadPicture/index.vue"

export default {
    name: "teacherList",
    components: {
        uploadPicture,
    },
    data() {
        return {
            datas: [],
            dialogVisible: false,
            dialogVisibleType: "",
            formData: {
                id: "",
                avatar: "",
                name: "",
                title: "",
                desc: "",
            },
        };
    },

    computed: {
        dialogText() {
            if (this.dialogVisibleType === "edit") {
                return "编辑讲师";
            }
            return "添加讲师";
        },

        title() {
            const { title } = this.$route.query;
            return title;
        },
    },

    created() {
        this.handleDatas();
    },
    methods: {
        async handleDatas() {
            const loading = this.$loading();
            try {
                const { topicId } = this.$route.query;

                const { data } = await this.$fetch.get("/api/topic-teacher/list", {
                    topicId,
                });
                this.datas = data;
                console.log(data);
            } catch (error) {
                console.log(error);
            }
            loading.close();
        },
        refresh() {
            this.handleDatas();
        },

        openDialog(type, row) {
            this.dialogVisibleType = type;
            this.initDialog();
            if (type === "edit") {
                this.formData = {
                    ...this.formData,
                    ...row,
                };
            }
            this.dialogVisible = true;
        },

        initDialog() {
            Object.keys(this.formData).forEach((item) => {
                this.$set(this.formData, item, "");
            });
        },

        cancel() {
            this.dialogVisible = false;
            this.refresh();
        },

        handleDelete(row) {
            const params = {
                id: row.id,
            };
            this.$confirm("确定要删除此讲师？", "", {
                distinguishCancelAndClose: true,
                confirmButtonText: "确认",
                cancelButtonText: "取消",
            }).then(async () => {
                const { code } = await this.$fetch.post(
                    "/api/topic-teacher/delete",
                    params
                );
                if (code === 0) {
                    this.$message.success("删除成功");
                }
                this.refresh();
            });
        },

        async handleUpdate() {
            let res;
            const { topicId } = this.$route.query;

            const { name, avatar } = this.formData

            const rules = [
                {
                    case: !name,
                    message: "请输入讲师姓名",
                },
                {
                    case: !avatar,
                    message: "请上传头像",
                },
            ]

            const valid = this.$validForm(rules)

            if (!valid) {
                return
            }

            if (this.dialogVisibleType === "edit") {
                res = await this.$fetch.post("/api/topic-teacher/update", {
                    ...this.formData,
                    topicId,
                });
            } else {
                res = await this.$fetch.post("/api/topic-teacher/create", {
                    ...this.formData,
                    topicId,
                });
            }

            if (res.code === 0) {
                this.$message.success(
                    this.dialogVisibleType === "edit" ? "修改成功" : "添加成功"
                );
            }

            this.cancel();
        },

        goBack() {
            this.$router.go(-1);
        },
    },
};
</script>

<style scoped lang="scss">
.teacher {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 50px);

    .title {
        font-size: 26px;
        width: 100%;
        line-height: 42px;
        justify-content: center;
        align-items: center;
        display: flex;
    }

    .teacher-header {
        height: 40px;
        display: flex;
        align-items: center;
        margin-top: 30px;
    }

    .teacher__content {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;
        padding-bottom: 0;

        .table-container {
            flex: 1;
            overflow: hidden;
            overflow-y: auto;
        }
    }

    .pagination-container {
        display: flex;
        justify-content: center;
        padding: 20px 0;

        .el-pagination {
            padding: 20px 0;
        }
    }
}

::v-deep .preview-image {
    width: 160px;
    height: 160px;
}

::v-deep .el-upload-dragger {
    width: 160px;
    height: 160px;
}
</style>
